<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="审核状态" prop="blogStatus">
        <el-select v-model="queryParams.blogStatus" placeholder="请选择1:草稿，2：审核中，3审核通过" clearable>
          <el-option
            v-for="dict in dict.type.blog_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:blog:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:blog:edit']"
        >审核</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-trophy-1"
          size="mini"
          :disabled="multiple"
          @click="handleApplyIds"
        >审核</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:blog:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="博文id" align="center" prop="blogId" />
      <el-table-column label="aaa" align="center" prop="blogTitle" />
      <el-table-column label="aaa" align="center" prop="blogDesc" />
      <el-table-column label="博文内容" align="center" prop="blogContent" />
      <el-table-column label="FK用户id" align="center" prop="userId" />
      <el-table-column label="1:草稿，2：审核中，3审核通过" align="center" prop="blogStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.blog_status" :value="scope.row.blogStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="blog_status=4时原因" align="center" prop="blogRetryReason" />
      <el-table-column label="FK专栏id" align="center" prop="colId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)" plain>预览</el-button>
          <el-button size="mini" type="text" @click="handleApply(scope.row)"  plain>审核</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:blog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:blog:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改blog对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="aaa" prop="blogTitle">
          <el-input v-model="form.blogTitle" placeholder="请输入aaa" />
        </el-form-item>
        <el-form-item label="aaa" prop="blogDesc">
          <el-input v-model="form.blogDesc" placeholder="请输入aaa" />
        </el-form-item>
        <el-form-item label="博文内容">
          <editor v-model="form.blogContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="FK用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入FK用户id" />
        </el-form-item>
        <el-form-item label="1:草稿，2：审核中，3审核通过" prop="blogStatus">
          <el-radio-group v-model="form.blogStatus">
            <el-radio
              v-for="dict in dict.type.blog_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="blog_status=4时原因" prop="blogRetryReason">
          <el-input v-model="form.blogRetryReason" placeholder="请输入blog_status=4时原因" />
        </el-form-item>
        <el-form-item label="FK专栏id" prop="colId">
          <el-input v-model="form.colId" placeholder="请输入FK专栏id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {blogApplyApiIds, blogApplyApi, listBlog, listBlog1, getBlog, delBlog, addBlog, updateBlog } from "@/api/system/blog";

export default {
  name: "Blog",
  dicts: ['blog_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // blog表格数据
      blogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        blogTitle: null,
        blogDesc: null,
        blogContent: null,
        userId: null,
        blogStatus: '2',
        blogRetryReason: null,
        colId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        blogStatus: [
          { required: true, message: "1:草稿，2：审核中，3审核通过不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    // 数据初始化之后
    this.getList();
  },
  methods: {
    /** 查询blog列表 */
    getList() {
      this.loading = true;
      // .then(()=>{}).then(()=>{}).catch(()=>{}).finally(()=>{})  promise(reject,resolve)
      // listBlog(this.queryParams).then(response => {
      //   this.blogList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      listBlog1({blogStatus: this.queryParams.blogStatus}).then((response ) => {
        this.blogList = response.rows;
        this.total = response.total;
      }).finally(()=>{
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        blogId: null,
        blogTitle: null,
        blogDesc: null,
        blogContent: null,
        userId: null,
        createBy: null,
        blogStatus: null,
        blogRetryReason: null,
        updateBy: null,
        updateTime: null,
        createTime: null,
        colId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.blogId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加blog";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const blogId = row.blogId || this.ids
      getBlog(blogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改blog";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.blogId != null) {
            updateBlog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBlog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const blogIds = row.blogId || this.ids;
      this.$modal.confirm('是否确认删除blog编号为"' + blogIds + '"的数据项？').then(function() {
        return delBlog(blogIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/blog/export', {
        ...this.queryParams
      }, `blog_${new Date().getTime()}.xlsx`)
    },
    handleApply(row) {
      let param = {blogId: row.blogId, blogStatus: 3}
      
      this.$modal.confirm('是否确认审核"' + row.blogId + '"的数据项？').then(function() {
        blogApplyApi(param).then(r => {
          
        })
      }).then(() => {
        this.$modal.msgSuccess("审核成功");
        this.getList()
      }).catch(() => {});
    },
    handleApplyIds() {
      if (this.ids.length == 0) {
         this.$message.error("没有选择的记录")
         return
      }
      let params = this.ids.map(item => {return {blogId: item, blogStatus: 3}})
      this.$modal.confirm('是否确认审核"' + this.ids + '"的数据项？').then(function() {
        blogApplyApiIds(params).then(r => {
          
        })
      }).then(() => {
        this.$modal.msgSuccess("审核成功");
        this.getList()
      }).catch(() => {});
    }
  }
};
</script>
